package com.didi.gulimall;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
import com.baomidou.mybatisplus.generator.config.GlobalConfig;
import com.baomidou.mybatisplus.generator.config.PackageConfig;
import com.baomidou.mybatisplus.generator.config.StrategyConfig;
import com.baomidou.mybatisplus.generator.config.rules.DateType;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import lombok.extern.slf4j.Slf4j;
import org.junit.Test;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

@Slf4j
public class CreatCode {

    @Test
    public void run() {

        // 1、创建代码生成器
        AutoGenerator mpg = new AutoGenerator();

        // 2、全局配置
        GlobalConfig gc = new GlobalConfig();
        String projectPath = System.getProperty("user.dir");
        gc.setSwagger2(true); //实体属性 Swagger2 注解
        gc.setOutputDir("F:\\Desktop\\Idea\\gulimallparent\\gulimall-member" + "/src/main/java");

        gc.setAuthor("1062");
        gc.setOpen(false); //生成后是否打开资源管理器
        gc.setFileOverride(false); //重新生成时文件是否覆盖

        //UserServie
        gc.setServiceName("%sService");    //去掉Service接口的首字母I
        gc.setIdType(IdType.AUTO); //主键策略
        gc.setDateType(DateType.ONLY_DATE);//定义生成的实体类中日期类型
        gc.setSwagger2(true);//开启Swagger2模式

        mpg.setGlobalConfig(gc);

        // 3、数据源配置
        DataSourceConfig dsc = new DataSourceConfig();
        dsc.setUrl("jdbc:mysql://localhost:3306/gulimall-ums?serverTimezone=GMT%2B8");
        dsc.setDriverName("com.mysql.cj.jdbc.Driver");
        dsc.setUsername("root");
        dsc.setPassword("root");
        dsc.setDbType(DbType.MYSQL);
        mpg.setDataSource(dsc);

        // 4、包配置
        PackageConfig pc = new PackageConfig();
        pc.setParent("com.didi.gulimall");
        //包  com.atguigu.eduservice
        pc.setModuleName("member"); //模块名
        //包  com.atguigu.eduservice.controller
        pc.setController("controller");
        pc.setEntity("entity");
        pc.setService("service");
        pc.setMapper("dao");
        mpg.setPackageInfo(pc);

        // 5、策略配置
        StrategyConfig strategy = new StrategyConfig();


        String[] table = {"ums_member_order_task"};

        strategy.setInclude(table);


        strategy.setNaming(NamingStrategy.underline_to_camel);//数据库表映射到实体的命名策略
//        strategy.setTablePrefix("t" + "_"); //生成实体时去掉表前缀
        String moduleName = pc.getModuleName();
        System.out.println("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" + moduleName);
        strategy.setTablePrefix("ums" + "_"); //生成实体时去掉表前缀
        strategy.setColumnNaming(NamingStrategy.underline_to_camel);//数据库表字段映射到实体的命名策略
        strategy.setEntityLombokModel(true); // lombok 模型 @Accessors(chain = true) setter链式操作

        strategy.setRestControllerStyle(true); //restful api风格控制器
        strategy.setControllerMappingHyphenStyle(true); //url中驼峰转连字符

        mpg.setStrategy(strategy);
        // 6、执行
        mpg.execute();
    }

    @Test
    public void test() {
//        String ret = "0001  03 04  0d 00 00 00 a394";
        String ret = "000103040d000000a394";
        String actual = ret.substring(4, 6);
        if (actual.equals("03")) {
            return;
        }
        String ret1 = ret.substring(8, 16);
        System.out.println("反转之前的结果" + ret1);
        String reverse = reverse(ret1);
        System.out.println("反转之后的结果" + reverse);
        int i = Integer.parseInt(reverse, 16);
        double record = i / 100.00;
        System.out.println("最后的结果" + record);
    }

    private static String reverse(String source) {
        int z = source.length() / 2;
        StringBuilder stringBuilder = new StringBuilder();
        for (int i = 0; i < source.length(); i += 2) {
            String substring = source.substring(i, i + 2);
            stringBuilder.insert(0, substring);
        }
        return stringBuilder.toString();
    }

    @Test
    public void testDate() {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        String previousStr = "2021-05-04";
        String dateStr = "2021-05-08";
        try {
            System.out.println(determineDate(format.parse(previousStr), format.parse(dateStr)));
        } catch (ParseException e) {
            e.printStackTrace();
        }
    }

    private boolean determineDate(Date previous, Date gmtCreate) {
        Calendar calPrevious = Calendar.getInstance();
        Calendar calDate = Calendar.getInstance();
        calPrevious.setTime(previous);
        calDate.setTime(gmtCreate);
        System.out.println(calPrevious.get(Calendar.DAY_OF_YEAR));
        System.out.println(calPrevious.get(Calendar.YEAR));
        return calPrevious.get(Calendar.YEAR) == calDate.get(Calendar.YEAR) &&
                calPrevious.get(Calendar.DAY_OF_YEAR) == calDate.get(Calendar.DAY_OF_YEAR);
    }
}
